
var _messageOptions={
    detailUrl:"message/notice/detail/",
    listUrl:"message/notice/userList",
    interval: 2000
}

function getMessageStr(msg) {
    return "<li class='message-box'><a data-noticeid=\""+msg.noticeId+"\" class=\"messageItem\">" +
        "<span class=\"message-title\">"+msg.noticeTitle+"</span>" +
        "<div><i class='fa fa-clock-o'></i><span class=\"message-time\">"+msg.createTime+"</span></div>" +
        "</a></li>";
}

//计算元素集合的总宽度
function calSumWidth(elements) {
    var width = 0;
    $(elements).each(function() {
        width += $(this).outerWidth(true);
    });
    return width;
}

//滚动到指定选项卡
function scrollToTab(element) {
    var marginLeftVal = calSumWidth($(element).prevAll()),
        marginRightVal = calSumWidth($(element).nextAll());
    // 可视区域非tab宽度
    var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".menuTabs"));
    //可视区域tab宽度
    var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
    //实际滚动宽度
    var scrollVal = 0;
    if ($(".page-tabs-content").outerWidth() < visibleWidth) {
        scrollVal = 0;
    } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
        if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
            scrollVal = marginLeftVal;
            var tabElement = element;
            while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
                scrollVal -= $(tabElement).prev().outerWidth();
                tabElement = $(tabElement).prev();
            }
        }
    } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
        scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
    }
    $('.page-tabs-content').animate({
            marginLeft: 0 - scrollVal + 'px'
        },
        "fast");
}

function buildMessageItem() {
    // 获取标识数据
    var dataUrl = _messageOptions.detailUrl+$(this).data('noticeid'),
        dataIndex = $(this).data('index'),
        menuName = $.trim($(this).text()),
        flag = true;
    $(".nav ul li, .nav li").removeClass("selected");
    $(this).parent("li").addClass("selected");
    if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;

    // 选项卡菜单已存在
    $('.menuTab').each(function() {
        if ($(this).data('id') == dataUrl) {
            flag = false;
            return false;
        }
    });

    // 选项卡菜单不存在
    if (flag) {
        setTimeout(function(){fetchNewMessage()},2000);

        var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
        $('.menuTab').removeClass('active');

        // 添加选项卡对应的iframe
        var str1 = '<iframe class="RuoYi_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
        $('.mainContent').find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);

        $.modal.loading("数据加载中，请稍后...");

        $('.mainContent iframe:visible').load(function () {
            $.modal.closeLoading();
        });

        // 添加选项卡
        $('.menuTabs .page-tabs-content').append(str);
        scrollToTab($('.menuTab.active'));

        // 刷新消息
    }
    return false;
}

function buildEvent() {
    $(".messageItem").each(function(index) {
        if (!$(this).attr('data-index')) {
            $(this).attr('data-index', index);
            $(this).on('click', buildMessageItem);
        }
    });
}

function changeList(count) {
    if(count == 0){
        $('#message_count').hide();
        $('#no_message_box').show();
    }else{
        $('#message_count').show();
        $('#no_message_box').hide();
    }
}

/*获取新的消息通知*/
function fetchNewMessage(){
    var messageCount = parseInt($('#message_count').text());
    $.post(_messageOptions.listUrl,{isRead:0,pageSize:5,pageNum:1,orderByColumn:"createTime",isAsc:"desc"},function(result){
        var count = result.rows.length;
        if(messageCount != count){
            $('#message_count').html(count);
            changeList(count);
            $('#message_box').children('.message-box').remove();
            while (count > 0) {
                $('#message_box').prepend(getMessageStr(result.rows[--count]));
            }
            buildEvent();
        }
    });
}

function messageInit(messageOptions){
    if(messageOptions != undefined){
        if(messageOptions.detailUrl != undefined){
            _messageOptions.detailUrl = messageOptions.detailUrl;
        }
        if(messageOptions.listUrl != undefined){
            _messageOptions.listUrl = messageOptions.listUrl;
        }
        if(messageOptions.interval != undefined) {
            _messageOptions.interval = messageOptions.interval;
        }
    }
    buildEvent();
    changeList(parseInt($('#message_count').text()));
    setInterval(function(){fetchNewMessage()},_messageOptions.interval);
}
